﻿function HandlePageCustomer(gui, operator, now, options) {
  this._gui = gui;
  this._operator = operator;
  this._now = now;
  this._options = options;
  this._popupBox = new PopupBox();
  this._popupBox2 = new PopupBox2();
  this.originalorder =[];
  
  this._customerId = (options && options.id) ? options.id : null;
  this._orderTrackId = (options && options.track) ? options.track : null;
  this._visitor = (options && options.visitor) ? options.visitor : null;
  this._exist1 = false; this._exist2 = false;
  this._exist3 = false; this._exist4 = false;
  this._gui.order.add.style.display = 'none';
  this._gui.customer.rc.style.visibility = 'hidden';
  DlmanageUtils.initialise(document.body);
  this._createElements();
};

HandlePageCustomer.prototype._createElements = function() {
  
  var pos = [window.screen.width / 2, window.screen.height / 5];
  MiscUtils.load(this._popupBox, pos, ' Loading Massive Data... ');
  
  this._loadData();
};

HandlePageCustomer.prototype._loadData = function() {
  this._customer = null;
  this._samples = null;
  this._packages = null;
  this._sources = null;
  this._receptions = null;
  this._cultures = null;
  this._ceremonys = null;
  this._produces = null;

  
  this._retrieveCustomer();
   
  this._retrieveSamples();
  this._retrievePackages();
  this._retrieveSources();
  this._retrieveReceptions();
  this._retrieveCultures();
  this._retrieveCeremonys();
  this._retrieveProduces();

  
};

HandlePageCustomer.prototype._verifyData = function() {

  if (this._customer && this._samples && this._packages && this._sources && this._receptions && this._cultures && this._ceremonys && this._produces) {
     
      this._updateElements();
  }
};

HandlePageCustomer.prototype._retrieveCustomer = function() {
  var _self = this;
  if (this._customerId) {
    /*new RequestUtils()._read('customer', null, 'd.oid =' + this._customerId, null, null, null, null, function(result, params) { _self._customer = (result.data.length == 1) ? result.data[0] : null;
                                                                                                                                _self._verifyData.call(_self);
                                                                                                                              }, null);
  */
   var args = '&oid=' + this._customerId; 
  new RequestUtils()._mysql('v1customer', args, function(result, params) { _self._customer = (result.data.length == 1) ? result.data[0] : null;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});     
     
  
  
  } else {
    this._customer = v1customer.instance();
    this._customer.orders = [];
    this._customer.createdDate = this._now;
    this._customer.sales = this._operator;
    
    this._verifyData();
  }
};

HandlePageCustomer.prototype._retrieveSamples = function(page, condition, date, from, to, pos) {
  var _self = this;
  /*new RequestUtils()._read('sample', null, '', null, null, null, null, function(result, params) { _self._samples = result.data;
                                                                                                  _self._verifyData.call(_self);
                                                                                                }, null);
*/
  new RequestUtils()._mysql('v1sample_s', null, function(result, params) { _self._samples = result.data;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});  
};

HandlePageCustomer.prototype._retrievePackages = function(page, condition, date, from, to, pos) {
  var _self = this;
  /*new RequestUtils()._read('package', null, null, null, null, null, null, function(result, params) { _self._packages = result.data;
                                                                                                     _self._verifyData.call(_self);
                                                                                                   }, null);
*/

  new RequestUtils()._mysql('v1package_s', null, function(result, params) { _self._packages = result.data;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});  
};

HandlePageCustomer.prototype._retrieveSources = function(page, condition, date, from, to, pos) {
  var _self = this;
  /*new RequestUtils()._read('isource', null, null, null, null, null, null, function(result, params) { _self._sources = result.data;
                                                                                                     _self._verifyData.call(_self);
                                                                                                   }, null);
*/
  new RequestUtils()._mysql('isource', null, function(result, params) { _self._sources = result.data;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});  


};

HandlePageCustomer.prototype._retrieveReceptions = function(page, condition, date, from, to, pos) {
  var _self = this;
  /*new RequestUtils()._read('ireception', null, null, null, null, null, null, function(result, params) { _self._receptions = result.data;
                                                                                                        _self._verifyData.call(_self);
                                                                                                      }, null);
*/
  new RequestUtils()._mysql('ireception', null, function(result, params) { _self._receptions = result.data;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});  
};

HandlePageCustomer.prototype._retrieveCultures = function(page, condition, date, from, to, pos) {
  var _self = this;
  /*new RequestUtils()._read('iculture', null, null, null, null, null, null, function(result, params) { _self._cultures = result.data;
                                                                                                      _self._verifyData.call(_self);
                                                                                                    }, null);
*/

  new RequestUtils()._mysql('iculture', null, function(result, params) { _self._cultures = result.data;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});  
};

HandlePageCustomer.prototype._retrieveCeremonys = function(page, condition, date, from, to, pos) {
  var _self = this;
  /*
  new RequestUtils()._read('iceremony', null, null, null, null, null, null, function(result, params) { _self._ceremonys = result.data;
                                                                                                       _self._verifyData.call(_self);
                                                                                                     }, null);
*/
  new RequestUtils()._mysql('iceremony', null, function(result, params) { _self._ceremonys = result.data;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});  
};

HandlePageCustomer.prototype._retrieveProduces = function() {
  var _self = this;
  if (this._customerId) {
    var joins = '&selects=1' + '&purchaseitems=1' + '&preconusers=0' + '&events=1' + '&tracks=0' + '&produces=0' + '&notes=1';
    var args = '&c=d.branch = \'{{$smarty.const.BRANCH|escape:javascript}}\' AND d.customerId = ' + this._customerId + joins;
    new RequestUtils()._mysql('produce', args, function(result, params) { _self._produces = result.data;
                                                                          _self._verifyData.call(_self);
                                                                        }, { php: Constant.FACTORY + '/customphp/produceread.php'});
  } else {
    this._produces = [];
  }
};

HandlePageCustomer.prototype._updateElements = function() {
  var _self = this, table, tr, td;
  this._popupBox._close();
  DlmanageUtils.clear(this._gui);

  
  /* 初始化Customer的Order */
  if (this._customer.orders.length == 0) {
    var o = v1myorder.instance();
    o.createdDate = this._now;
    o.flag = DlmanageUtils.flag(this._customer, Constant.ORDER_TYPE_WEDDING);
    o.type = Constant.ORDER_TYPE_WEDDING;
    o.date = null;
    o.purchaseItems = [];
    o.payments = [];

    this._customer.orders[0] = o;
    
    var produce = Produce.instance();
    produce.branch = '{{$smarty.const.BRANCH|escape:javascript}}';
    produce.host = 'http://{{$smarty.const.DOMAIN|escape:javascript}}{{$smarty.const.PATH|escape:javascript}}';
    produce.title = 'Pre Con';
    produce.when = 0;
    produce.orderTrackId = o.trackId;
    produce.pi_trackId = MiscUtils.unique();
    produce.pi_model = '{{$smarty.const.MODEL_NAME_PRECON|escape:javascript}}';
    produce.pi_selects = [];
    this._produces[this._produces.length] = produce;
	
  }
  /* 初始化页面的OrderTrackId */
  MiscUtils.sort(this._customer.orders, ['id']);
  this._orderTrackId = (this._orderTrackId) ? this._orderTrackId : this._customer.orders[0].trackId;
  
  /* 显示Customer的Id */
  if (this._customerId) {
    table = document.createElement('table');
    table.cellPadding = 0;
    table.cellSpacing = 0;
    table.style.width = '900px';
    table.style.height = '25px';
    table.style.margin = '0 0 5px 0';
    this._gui.customerNo.appendChild(table);
    
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.width = '745px';
    td.style.paddingRight = '5px';
    td.style.fontWeight = 'bold';
    td.style.fontSize = '140%';
    td.style.color = '#694247';
    td.style.textAlign = 'right';
    td.style.verticalAlign = 'middle';
    td.appendChild(document.createTextNode('Customer No.'));
    td = tr.insertCell(-1);
    td.style.backgroundColor = '#f2e6d9';
    td.style.width = '150px';
    td.style.height = '25px';
    td.style.textAlign = 'center';
    td.style.verticalAlign = 'middle';
    td.style.fontSize = '16px';
    td.style.fontWeight = 'bold';
    td.color = '#000';
    td.appendChild(document.createTextNode(DlmanageUtils.customerId(this._customerId)));
  }
  
  /* 显示Customer的联系方式 */
  /* - 从Visitor传递过来的Customer信息  - */
  if (this._visitor && this._visitor.fromVisitor) {
    this._gui.customer.bridename.value = this._visitor.brideName;
    this._gui.customer.brideaddrLn1.value = this._visitor.brideAddress;
    this._gui.customer.bridephone.value = this._visitor.bridePhone;
    this._gui.customer.bridemobile.value = this._visitor.brideMobile;
    this._gui.customer.brideemail.value = this._visitor.brideEmail;
    this._gui.customer.groomname.value = this._visitor.groomName;
    this._gui.customer.groomaddrLn1.value = this._visitor.groomAddress;
    this._gui.customer.groomphone.value = this._visitor.groomPhone;
    this._gui.customer.groommobile.value = this._visitor.groomMobile;
    this._gui.customer.groomemail.value = this._visitor.groomEmail;
  } else {
    this._gui.customer.bridename.value = this._customer.bridename;
    this._gui.customer.brideaddrLn1.value = this._customer.brideaddrLn1;
    this._gui.customer.brideaddrLn2.value = this._customer.brideaddrLn2;
    this._gui.customer.bridephone.value = this._customer.bridephone;
    this._gui.customer.bridemobile.value = this._customer.bridemobile;
    this._gui.customer.brideemail.value = this._customer.brideemail;
    this._gui.customer.groomname.value = this._customer.groomname;
    this._gui.customer.groomaddrLn1.value = this._customer.groomaddrLn1;
    this._gui.customer.groomaddrLn2.value = this._customer.groomaddrLn2;
    this._gui.customer.groomphone.value = this._customer.groomphone;
    this._gui.customer.groommobile.value = this._customer.groommobile;
    this._gui.customer.groomemail.value = this._customer.groomemail;
    this._gui.officeNote.value = this._customer.note;
    this._gui.reminderNote.value = this._customer.reminderNote;
  }
  /* 设置Customer的联系方式Event */
  this._gui.customer.bridename.onchange = function() { _self._customer.bridename = this.value; };
  this._gui.customer.brideaddrLn1.onchange = function() { _self._customer.brideaddrLn1 = this.value; };
  this._gui.customer.brideaddrLn2.onchange = function() { _self._customer.brideaddrLn2 = this.value; };
  this._gui.customer.bridephone.onchange = function() { _self._customer.bridephone = this.value; };
  this._gui.customer.bridemobile.onchange = function() { _self._customer.bridemobile = this.value; };
  this._gui.customer.brideemail.onchange = function() { _self._customer.brideemail = this.value; };
  this._gui.customer.groomname.onchange = function() { _self._customer.groomname = this.value; };
  this._gui.customer.groomaddrLn1.onchange = function() { _self._customer.groomaddrLn1 = this.value; };
  this._gui.customer.groomaddrLn2.onchange = function() { _self._customer.groomaddrLn2 = this.value; };
  this._gui.customer.groomphone.onchange = function() { _self._customer.groomphone = this.value; };
  this._gui.customer.groommobile.onchange = function() { _self._customer.groommobile = this.value; };
  this._gui.customer.groomemail.onchange = function() { _self._customer.groomemail = this.value; };
  this._gui.officeNote.onchange = function() { _self._customer.note = this.value; };
  this._gui.reminderNote.onchange = function() { _self._customer.reminderNote = this.value; };
  /* 显示Source等信息 */
  
  this._gui.customer.source.name.options[this._gui.customer.source.name.options.length] = new Option('');
  for (var i = 0, il = this._sources.length; i < il; i++) {
    var source = this._sources[i];
    var option = new Option(source.name);
    this._gui.customer.source.name.options[this._gui.customer.source.name.options.length] = option;
    if (option.text == this._customer.source) {
      this._gui.customer.source.name.selectedIndex = this._gui.customer.source.name.options.length - 1;
    }
  }
  this._gui.customer.reception.name.options[this._gui.customer.reception.name.options.length] = new Option('');
  for (var i = 0, il = this._receptions.length; i < il; i++) {
    var reception = this._receptions[i];
    var option = new Option(reception.name);
    this._gui.customer.reception.name.options[this._gui.customer.reception.name.options.length] = option;
    if (option.text == this._customer.reception){
      this._gui.customer.reception.name.selectedIndex = this._gui.customer.reception.name.options.length - 1;
    }
  }
  this._gui.customer.cultural.name.options[this._gui.customer.cultural.name.options.length] = new Option('');
  for (var i = 0, il = this._cultures.length; i < il; i++) {
    var culture = this._cultures[i];
    var option = new Option(culture.name);
    this._gui.customer.cultural.name.options[this._gui.customer.cultural.name.options.length] = option;
    if (option.text == this._customer.culture){
      this._gui.customer.cultural.name.selectedIndex = this._gui.customer.cultural.name.options.length - 1;
    }
  }
  this._gui.customer.cultural2.name.options[this._gui.customer.cultural2.name.options.length] = new Option('');
  for (var i = 0, il = this._cultures.length; i < il; i++) {
    var culture = this._cultures[i];
    var option = new Option(culture.name);
    this._gui.customer.cultural2.name.options[this._gui.customer.cultural2.name.options.length] = option;
    if (option.text == this._customer.culture2){
      this._gui.customer.cultural2.name.selectedIndex = this._gui.customer.cultural2.name.options.length - 1;
    }
  }
  this._gui.customer.ceremony.name.options[this._gui.customer.ceremony.name.options.length] = new Option('');
  for (var i = 0, il = this._ceremonys.length; i < il; i++) {
    var ceremony = this._ceremonys[i];
    var option = new Option(ceremony.name);
    this._gui.customer.ceremony.name.options[this._gui.customer.ceremony.name.options.length] = option;
    if (option.text == this._customer.ceremony){
      this._gui.customer.ceremony.name.selectedIndex = this._gui.customer.ceremony.name.options.length - 1;
    }
  }
  
  /* 设置Source等信息Event */
  if (this._sources.length > 0) {
    this._customer.source = this._gui.customer.source.name.options[this._gui.customer.source.name.selectedIndex].text;
    this._gui.customer.source.name.onchange = function() { _self._customer.source = this.options[this.selectedIndex].text; };
  }
  if (this._receptions.length > 0) {
    this._customer.reception = this._gui.customer.reception.name.options[this._gui.customer.reception.name.selectedIndex].text;
    this._gui.customer.reception.name.onchange = function() { _self._customer.reception = this.options[this.selectedIndex].text; };
  }
  if (this._cultures.length > 0) {
    this._customer.culture = this._gui.customer.cultural.name.options[this._gui.customer.cultural.name.selectedIndex].text;
    this._gui.customer.cultural.name.onchange = function() { _self._customer.culture = this.options[this.selectedIndex].text; };
  }
  if (this._cultures.length > 0) {
    this._customer.culture2 = this._gui.customer.cultural2.name.options[this._gui.customer.cultural2.name.selectedIndex].text;
    this._gui.customer.cultural2.name.onchange = function() { _self._customer.culture2 = this.options[this.selectedIndex].text; };
  }
  if (this._ceremonys.length > 0) {
    this._customer.ceremony = this._gui.customer.ceremony.name.options[this._gui.customer.ceremony.name.selectedIndex].text;
    this._gui.customer.ceremony.name.onchange = function() { _self._customer.ceremony = this.options[this.selectedIndex].text; };
  }
  /* - 从Visitor传递过来的Source等信息  - */
  if (this._visitor && this._visitor.fromVisitor) {
    for (var i = 0, il = this._cultures.length; i < il; i++) {
      var culture = this._cultures[i];
      if (culture.name == this._visitor.culture || this._visitor.culture == '') {
        this._exist1 = true;
        this._customer.culture = this._visitor.culture;
      }
    }
    for (var i = 0, il = this._ceremonys.length; i < il; i++) {
      var ceremony = this._ceremonys[i];
      if (ceremony.name == this._visitor.ceremony || this._visitor.ceremony == '') {
        this._exist2 = true;
        this._customer.ceremony = this._visitor.ceremony;
      }
    }
    for (var i = 0, il = this._receptions.length; i < il; i++) {
      var reception = this._receptions[i];
      if (reception.name == this._visitor.reception || this._visitor.reception == '') {
        this._exist3 = true;
        this._customer.reception = this._visitor.reception;
      }
    }
    for (var i = 0, il = this._sources.length; i < il; i++) {
      var source = this._sources[i];
      if (source.name == this._visitor.source || this._visitor.source == '') {
        this._exist4 = true;
        this._customer.source = this._visitor.source;
      }
    }
    if (!this._exist1) {
      var r = window.confirm('Automatically add culture background "' + this._visitor.culture + '", continue?');
      if (r) {
        this._customer.culture = this._visitor.culture;
        var object = ICulture.instance();
        object.name = this._visitor.culture;
        new RequestUtils()._mwrite('iculture', [object], [], function() { _self._retrieveCultures.call(_self); }, { pos: DOMUtils.findPos(_self._gui.customer.cultural.add) });
      } else {
        this._exist1 = true;
        _self._updateElements.call(_self);
      }
    } else if (!this._exist2) {
      var r = window.confirm('Automatically add ceremony location "' + this._visitor.ceremony + '", continue?');
      if (r) {
        this._customer.ceremony = this._visitor.ceremony;
        var object = ICeremony.instance();
        object.name = this._visitor.ceremony;
        new RequestUtils()._mwrite('iceremony', [object], [], function() { _self._retrieveCeremonys.call(_self); }, { pos: DOMUtils.findPos(_self._gui.customer.ceremony.add) });
      } else {
        this._exist2 = true;
        _self._updateElements.call(_self);
      }
    } else if (!this._exist3) {
      var r = window.confirm('Automatically add reception location "' + this._visitor.reception + '", continue?');
      if (r) {
        this._customer.reception = this._visitor.reception;
        var object = IReception.instance();
        object.name = this._visitor.reception;
        new RequestUtils()._mwrite('ireception', [object], [], function() { _self._retrieveReceptions.call(_self); }, { pos: DOMUtils.findPos(_self._gui.customer.reception.add) });
      } else {
        this._exist3 = true;
        _self._updateElements.call(_self);
      }
    } else if (!this._exist4) {
      var r = window.confirm('Automatically add source "' + this._visitor.source + '", continue?');
      if (r) {
        this._customer.source = this._visitor.source;
        var object = ISource.instance();
        object.name = this._visitor.source;
        new RequestUtils()._mwrite('isource', [object], [], function() { _self._retrieveSources.call(_self); }, { pos: DOMUtils.findPos(_self._gui.customer.source.add) });
      } else {
        this._exist4 = true;
        _self._updateElements.call(_self);
      }
    }
  }
  
  /* 添加Source等信息 */
  this._gui.customer.source.add.onclick = function() { var object = ISource.instance();
                                                       var div = document.createElement('div');
                                                       var func1 = function() { _self._popupBox._close();
                                                                                _self._customer.source = object.name;
                                                                                new RequestUtils()._mwrite('isource', [object], [], function() { _self._retrieveSources.call(_self); }, { pos: DOMUtils.findPos(_self._gui.customer.source.add) });
                                                                              };
                                                       var func2 = function() { _self._popupBox._close();
                                                                              };
                                                       MiscUtils.dialog(_self._popupBox, DOMUtils.findPos(this), div, func1, func2, { ok: 'Add' });
                                                       new DialogIObject(div, object, { name: 'isource', title: 'Sources' , php: 'customphp/mysqlread.php' });
                                                     };
  this._gui.customer.reception.add.onclick = function() { var object = IReception.instance();
                                                          var div = document.createElement('div');
                                                          var func1 = function() { _self._popupBox._close();
                                                                                   _self._customer.reception = object.name;
                                                                                   new RequestUtils()._mwrite('ireception', [object], [], function() { _self._retrieveReceptions.call(_self); }, { pos: DOMUtils.findPos(_self._gui.customer.reception.add) });
                                                                                 };
                                                          var func2 = function() { _self._popupBox._close();
                                                                                 };
                                                          MiscUtils.dialog(_self._popupBox, DOMUtils.findPos(this), div, func1, func2, { ok: 'Add' });
                                                          new DialogIObject(div, object, { name: 'ireception', title: 'Reception Locations' , php: 'customphp/mysqlread.php'  });
                                                        };
  this._gui.customer.cultural.add.onclick = function() { var object = ICulture.instance();
                                                         var div = document.createElement('div');
                                                         var func1 = function() { _self._popupBox._close();
                                                                                  _self._customer.culture = object.name;
                                                                                  new RequestUtils()._mwrite('iculture', [object], [], function() { _self._retrieveCultures.call(_self); }, { pos: DOMUtils.findPos(_self._gui.customer.cultural.add) });
                                                                                };
                                                         var func2 = function() { _self._popupBox._close();
                                                                                };
                                                         MiscUtils.dialog(_self._popupBox, DOMUtils.findPos(this), div, func1, func2, { ok: 'Add' });
                                                         new DialogIObject(div, object, { name: 'iculture', title: 'Cultural Backgrounds' , php: 'customphp/mysqlread.php'  });
                                                       };
  this._gui.customer.cultural2.add.onclick = function() { var object = ICulture.instance();
                                                         var div = document.createElement('div');
                                                         var func1 = function() { _self._popupBox._close();
                                                                                  _self._customer.culture2 = object.name;
                                                                                  new RequestUtils()._mwrite('iculture', [object], [], function() { _self._retrieveCultures.call(_self); }, { pos: DOMUtils.findPos(_self._gui.customer.cultural.add) });
                                                                                };
                                                         var func2 = function() { _self._popupBox._close();
                                                                                };
                                                         MiscUtils.dialog(_self._popupBox, DOMUtils.findPos(this), div, func1, func2, { ok: 'Add' });
                                                         new DialogIObject(div, object, { name: 'iculture', title: 'Cultural Backgrounds' , php: 'customphp/mysqlread.php'  });
                                                       };
  this._gui.customer.ceremony.add.onclick = function() { var object = ICeremony.instance();
                                                         var div = document.createElement('div');
                                                         var func1 = function() { _self._popupBox._close();
                                                                                  _self._customer.ceremony = object.name;
                                                                                  new RequestUtils()._mwrite('iceremony', [object], [], function() { _self._retrieveCeremonys.call(_self); }, { pos: DOMUtils.findPos(_self._gui.customer.ceremony.add) });
                                                                                };
                                                         var func2 = function() { _self._popupBox._close();
                                                                                };
                                                         MiscUtils.dialog(_self._popupBox, DOMUtils.findPos(this), div, func1, func2, { ok: 'Add' });
                                                         new DialogIObject(div, object, { name: 'iceremony', title: 'Ceremony Locations' , php: 'customphp/mysqlread.php'  });
                                                       };
  
  /* 添加新订单 */
  this._gui.order.orderNew.onclick = function() { _self._type = '';
                                                  var div = document.createElement('div');
                                                  var func1 = function() { if (_self._type == '') {
                                                                             window.alert('NO TYPE');
                                                                             return;
                                                                           }
                                                                           var o = v1myorder.instance();
                                                                           o.purchaseItems = [];
																		   o.payments = [];
                                                                           o.createdDate = _self._now;
                                                                           o.date = null;
                                                                           o.flag = DlmanageUtils.flag(_self._customer, _self._type);
                                                                           o.type = _self._type;
                                                                           if (o.type == Constant.ORDER_TYPE_WEDDING) {
                                                                              var produce = Produce.instance();
                                                                              produce.branch = '{{$smarty.const.BRANCH|escape:javascript}}';
                                                                              produce.host = 'http://{{$smarty.const.DOMAIN|escape:javascript}}{{$smarty.const.PATH|escape:javascript}}';
                                                                              produce.title = 'Pre Con';
                                                                              produce.when = 0;
                                                                              produce.orderTrackId = o.trackId;
                                                                              produce.pi_trackId = MiscUtils.unique();
                                                                              produce.pi_model = '{{$smarty.const.MODEL_NAME_PRECON|escape:javascript}}';
                                                                              produce.pi_selects = [];
                                                                              _self._produces[_self._produces.length] = produce;
                                                                           }
                                                                           _self._orderTrackId = o.trackId;
                                                                           _self._customer.orders[_self._customer.orders.length] = o;
                                                                           _self._updateOrders.call(_self);
                                                                           _self._popupBox._close();
                                                                         };
                                                  var func2 = function() { _self._popupBox._close(); };
                                                  MiscUtils.dialog(_self._popupBox, DOMUtils.findPos(this), div, func1, func2, null);
                                                  var func =  function(type) { _self._type = type; }
                                                  new DialogOrderType(div, null, { callbackFunc: func });
                                                };
  
  /* Save & Cancel */
  if (this._customerId) {
    this._gui.save.onclick = function() { var preExist = false;
                                          for (var i = 0, il = _self._customer.orders.length; i <il; i++) {
                                            var order = _self._customer.orders[i];
                                            if (order.type == '{{$smarty.const.ORDER_TYPE_PREWEDDING|escape:javascript}}') {
                                              preExist = true;
                                            }
                                          }
                                          if ((_self._customer.culture != '' || preExist) && _self._customer.source != '') {
                                            if (DlmanageUtils.modelCheck(_self._customer.orders)) {
                                              window.alert('CAN NOT HAVE MORE THAN ONE "Video Editing" AND "BluRay Video Editing"' );
                                              return;
                                            }
                                            var pos = DOMUtils.findPos(this);
                                            var str = DlmanageUtils.checkOrderDate(_self._customer.orders);
                                            
                                            if (str != '') {
                                              var r = window.confirm('NO WEDDING/WORKING DAY (' + str.substring(0, str.length - 2) +'), CONTINUE?');
                                              if (r) {

                                                 
                                                DlmanageUtils.trimcustomer(_self._customer);
                                                DlmanageUtils.updateSystemNote(_self._customer, _self._newresult, {user: _self._operator, date: _self._now});
                                                new RequestUtils()._pcustom('updatev1customer', '&m=' + encodeURIComponent(JSON.stringify(_self._customer)), function(result, params) { var customer = (result.data.length == 1) ? result.data[0] : null;
                                                                                                                                                                                     var func = function(result, params) { if (customer) {
                                                                                                                                                                                                                             if (_self._operator.type == '{{$smarty.const.USER_TYPE_MANAGER_ASSISTANT|escape:javascript}}') {
                                                                                                                                                                                                                               location.href = '?p=pageasst&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: _self._customer.id, track: _self._orderTrackId });
                                                                                                                                                                                                                             } else if (_self._operator.type == '{{$smarty.const.USER_TYPE_CUSTOMER_SERVICE|escape:javascript}}') {
                                                                                                                                                                                                                               location.href = '?p=pageserv&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: _self._customer.id, track: _self._orderTrackId }); 
                                                                                                                                                                                                                             } else if (_self._operator.type == '{{$smarty.const.USER_TYPE_ADMIN|escape:javascript}}') {
                                                                                                                                                                                                                               location.href = '?p=pageadmin&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: _self._customer.id, track: _self._orderTrackId }); 
                                                                                                                                                                                                                             } else {
                                                                                                                                                                                                                               location.href = '?p=pagemanager&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: _self._customer.id, track: _self._orderTrackId }); 
                                                                                                                                                                                                                             }
                                                                                                                                                                                                                           }
                                                                                                                                                                                                                         };
                                                                                                                                                                                     DlmanageUtils.patch(_self._newresult.produces, customer);
                                                                                                                                                                                     DlmanageUtils.submit2(_self._newresult, func, pos);
                                                                                                                                                                                   }, { pos: pos });

                                              }
                                            } else {
                                                var wdstr = DlmanageUtils.orderdate(_self.originalorder,_self._customer.orders);
                                               if(wdstr != '')
                                                 {
                                                    var cfm = window.confirm(wdstr+',CONTINUE?');
                                                    
                                                    if(cfm)
                                                    {                                            
                                              DlmanageUtils.trimcustomer(_self._customer);
                                              DlmanageUtils.updateSystemNote(_self._customer, _self._newresult, {user: _self._operator, date: _self._now});
                                              new RequestUtils()._pcustom('updatev1customer', '&m=' + encodeURIComponent(JSON.stringify(_self._customer)), function(result, params) { var customer = (result.data.length == 1) ? result.data[0] : null;
                                                                                                                                                                                      var func = function(result, params) { if (customer) {
                                                                                                                                                                                                                           if (_self._operator.type == '{{$smarty.const.USER_TYPE_MANAGER_ASSISTANT|escape:javascript}}') {
                                                                                                                                                                                                                             location.href = '?p=pageasst&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: _self._customer.id, track: _self._orderTrackId });
                                                                                                                                                                                                                           } else if (_self._operator.type == '{{$smarty.const.USER_TYPE_CUSTOMER_SERVICE|escape:javascript}}') {
                                                                                                                                                                                                                             location.href = '?p=pageserv&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: _self._customer.id, track: _self._orderTrackId }); 
                                                                                                                                                                                                                           } else if (_self._operator.type == '{{$smarty.const.USER_TYPE_ADMIN|escape:javascript}}') {
                                                                                                                                                                                                                             location.href = '?p=pageadmin&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: _self._customer.id, track: _self._orderTrackId }); 
                                                                                                                                                                                                                           } else {
                                                                                                                                                                                                                             location.href = '?p=pagemanager&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: _self._customer.id, track: _self._orderTrackId });
                                                                                                                                                                                                                           }
                                                                                                                                                                                                                         }
                                                                                                                                                                                                                       };
                                                                                                                                                                                                                       
                                                                                                                                                                                   DlmanageUtils.patch(_self._newresult.produces, customer);
                                                                                                                                                                                   
                                                                                                                                                                                   DlmanageUtils.submit2(_self._newresult, func, pos);
                                                                                                                                                                                 }, { pos: pos });
                                               }
                                              }else{
                                               DlmanageUtils.trimcustomer(_self._customer);
                                              DlmanageUtils.updateSystemNote(_self._customer, _self._newresult, {user: _self._operator, date: _self._now});
                                              new RequestUtils()._pcustom('updatev1customer', '&m=' + encodeURIComponent(JSON.stringify(_self._customer)), function(result, params) { var customer = (result.data.length == 1) ? result.data[0] : null;
                                                                                                                                                                                      var func = function(result, params) { if (customer) {
                                                                                                                                                                                                                           if (_self._operator.type == '{{$smarty.const.USER_TYPE_MANAGER_ASSISTANT|escape:javascript}}') {
                                                                                                                                                                                                                             location.href = '?p=pageasst&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: _self._customer.id, track: _self._orderTrackId });
                                                                                                                                                                                                                           } else if (_self._operator.type == '{{$smarty.const.USER_TYPE_CUSTOMER_SERVICE|escape:javascript}}') {
                                                                                                                                                                                                                             location.href = '?p=pageserv&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: _self._customer.id, track: _self._orderTrackId }); 
                                                                                                                                                                                                                           } else if (_self._operator.type == '{{$smarty.const.USER_TYPE_ADMIN|escape:javascript}}') {
                                                                                                                                                                                                                             location.href = '?p=pageadmin&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: _self._customer.id, track: _self._orderTrackId }); 
                                                                                                                                                                                                                           } else {
                                                                                                                                                                                                                             location.href = '?p=pagemanager&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: _self._customer.id, track: _self._orderTrackId });
                                                                                                                                                                                                                           }
                                                                                                                                                                                                                         }
                                                                                                                                                                                                                       };
                                                                                                                                                                                                                       
                                                                                                                                                                                   DlmanageUtils.patch(_self._newresult.produces, customer);
                                                                                                                                                                                   
                                                                                                                                                                                   DlmanageUtils.submit2(_self._newresult, func, pos);
                                                                                                                                                                                 }, { pos: pos });                                             
                                              
                                              }
                                            }
                                          } else {
                                            if (preExist && _self._customer.source == '') {
                                              window.alert('CAN NOT BE EMPTY (Source)');
                                              return;
                                            } else {
                                              window.alert('CAN NOT BE EMPTY (Cultural Background, Source)');
                                              return;
                                            }
                                          }
                                        };
    this._gui.cancel.onclick = function() { history.back(); };
    
  } else {
 
    this._gui.save.onclick = function() { var preExist = false;
                                          for (var i = 0, il = _self._customer.orders.length; i <il; i++) {
                                            var order = _self._customer.orders[i];
                                            if (order.type == '{{$smarty.const.ORDER_TYPE_PREWEDDING|escape:javascript}}') {
                                              preExist = true;
                                            }
                                          }
                                          if ((_self._customer.culture != '' || preExist) && _self._customer.source != '') {
                                            if (DlmanageUtils.modelCheck(_self._customer.orders)) {
                                              window.alert('CAN NOT HAVE MORE THAN ONE "Video Editing" AND "BluRay Video Editing"' );
                                              return;
                                            }
                                            var pos = DOMUtils.findPos(this);
                                            var str = DlmanageUtils.checkOrderDate(_self._customer.orders);
                                            if (str != '') {
                                              var r = window.confirm('NO WEDDING/WORKING DAY (' + str.substring(0, str.length - 2) +'), CONTINUE?');
                                              if (r) {
                                                DlmanageUtils.trimcustomer(_self._customer);
                                                DlmanageUtils.updateSystemNote(_self._customer, _self._newresult, {user: _self._operator, date: _self._now});
                                                new RequestUtils()._pcustom('v1customer', '&m=' + encodeURIComponent(JSON.stringify(_self._customer)), function(result, params) { var customer = (result.data.length == 1) ? result.data[0] : null;
                                                                                                                                                                                  if (customer) {
                                                                                                                                                                                    var func = function(result, params) { if (customer) {
                                                                                                                                                                                                                            if (_self._operator.type == '{{$smarty.const.USER_TYPE_MANAGER_ASSISTANT|escape:javascript}}') {
                                                                                                                                                                                                                              location.href = '?p=pageasst&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: customer.id, track: _self._orderTrackId });
                                                                                                                                                                                                                            } else if (_self._operator.type == '{{$smarty.const.USER_TYPE_CUSTOMER_SERVICE|escape:javascript}}') {
                                                                                                                                                                                                                              location.href = '?p=pageserv&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: customer.id, track: _self._orderTrackId }); 
                                                                                                                                                                                                                            } else if (_self._operator.type == '{{$smarty.const.USER_TYPE_ADMIN|escape:javascript}}') {
                                                                                                                                                                                                                              location.href = '?p=pageadmin&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: customer.id, track: _self._orderTrackId }); 
                                                                                                                                                                                                                            } else {
                                                                                                                                                                                                                              location.href = '?p=pagemanager&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: customer.id, track: _self._orderTrackId }); 
                                                                                                                                                                                                                            }
                                                                                                                                                                                                                          }
                                                                                                                                                                                                                        };
                                                                                                                                                                                    DlmanageUtils.patch(_self._newresult.produces, customer);
                                                                                                                                                                                    DlmanageUtils.submit2(_self._newresult, func, pos);
                                                                                                                                                                                  }
                                                                                                                                                                                }, { pos: pos });
                                              }
                                            } else {
                                             DlmanageUtils.trimcustomer(_self._customer);
                                             DlmanageUtils.updateSystemNote(_self._customer, _self._newresult, {user: _self._operator, date: _self._now});

                                              new RequestUtils()._pcustom('v1customer', '&m=' + encodeURIComponent(JSON.stringify(_self._customer)), function(result, params) { var customer = (result.data.length == 1) ? result.data[0] : null;
                                                                                                                                                                                if (customer) {
                                                                                                                                                                                  var func = function(result, params) {if (customer) {
                                                                                                                                                                                                                          if (_self._operator.type == '{{$smarty.const.USER_TYPE_MANAGER_ASSISTANT|escape:javascript}}') {
                                                                                                                                                                                                                            location.href = '?p=pageasst&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: customer.id, track: _self._orderTrackId });
                                                                                                                                                                                                                          } else if (_self._operator.type == '{{$smarty.const.USER_TYPE_CUSTOMER_SERVICE|escape:javascript}}') {
                                                                                                                                                                                                                            location.href = '?p=pageserv&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: customer.id, track: _self._orderTrackId }); 
                                                                                                                                                                                                                          } else if (_self._operator.type == '{{$smarty.const.USER_TYPE_ADMIN|escape:javascript}}') {
                                                                                                                                                                                                                            location.href = '?p=pageadmin&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: customer.id, track: _self._orderTrackId }); 
                                                                                                                                                                                                                          } else {
                                                                                                                                                                                                                            location.href = '?p=pagemanager&t=pagecustomerold&m=' + JSON.stringify({ a: 2, b: 1 }) + '&opts=' + JSON.stringify({ id: customer.id, track: _self._orderTrackId }); 
                                                                                                                                                                                                                          }
                                                                                                                                                                                                                        }
                                                                                                                                                                                                                      };
                                                                                                                                                                                  DlmanageUtils.patch(_self._newresult.produces, customer);
                                                                                                                                                                                  DlmanageUtils.submit2(_self._newresult, func, pos);
                                                                                                                                                                                }
                                                                                                                                                                              }, { pos: pos });
                                            }
                                          } else {
                                            if (preExist && _self._customer.source == '') {
                                              window.alert('CAN NOT BE EMPTY ( Source)');
                                            } else {
                                              window.alert('CAN NOT BE EMPTY (Cultural Background, Source)');
                                            }
                                          }
                                        };
    this._gui.cancel.onclick = function() { history.back(); };
  }
  
  this._updateOrders();
};

HandlePageCustomer.prototype._updateOrders = function() {
  var _self = this;
  
  DlmanageUtils.clear(this._gui.order);
  DlmanageUtils.clear(this._gui.payment);
  
  

  
  /* 显示订单的标签 */
  var func = function(trackId) { _self._orderTrackId = trackId; 
                                 _self._updateOrders.call(_self);
                               };
                               
  new IContainerTabOrders(this._gui.order.orderTab, this._customer.orders, { callbackFunc: func, trackId: this._orderTrackId, newTab: true });
  
  
  /* 显示所有订单的总价和打折率 */
  var total = 0;
  var original = 0;
  var received = 0;
  for (var i = 0, il = this._customer.orders.length; i < il; i++) {
    var order = this._customer.orders[i];
    if (order.disposalstatus == 0 ) {
      order.price = Math.max(order.price, 0);
      total += parseFloat(order.price, 10);
      original += parseFloat(DlmanageUtils.total(order), 10);
    }
    for (var j = 0, jl = order.payments.length; j < jl; j++) {
      var payment = order.payments[j];
      if (payment.cancelled == 1) {
        continue;
      }
      received += parseFloat(payment.amount, 10);
    }
  }
  total = total.toFixed(2);
  original = original.toFixed(2);
  
  received = received.toFixed(2);
  this._gui.payment.price.appendChild(document.createTextNode((total > 0) ? '$' + (total * parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10)).toFixed(2) + ' (Inc. ' + '{{$smarty.const.TAX|escape:javascript}}' + ')' : '$0.00'));
  
  if (parseFloat(total) != parseFloat(original) && parseFloat(total) < parseFloat(original)) {
    this._gui.payment.percent.appendChild(document.createTextNode(' ' + (((original - total) / original) * 100).toFixed(1) + '% Off' ));
  }
  if (parseFloat(total) != parseFloat(original) && parseFloat(total) > parseFloat(original)) {
    this._gui.payment.percent.appendChild(document.createTextNode(' ' + 0 + '% Off' ));
  }  
  
  this._gui.payment.received.appendChild(document.createTextNode('$' + received));
  this._gui.payment.anticipated.appendChild(document.createTextNode('$' + ((total * parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10)) - received).toFixed(2)));
  
  /* 处理订单 */
  this._updateOrder();
}; 

HandlePageCustomer.prototype._updateOrder = function() {
  var table, tr, input, td, order = DlmanageUtils.order(this._customer.orders, this._orderTrackId), _self = this;
  
  if (order.type == Constant.ORDER_TYPE_PREWEDDING || order.type == Constant.ORDER_TYPE_PORTRAIT || order.type == Constant.ORDER_TYPE_OTHER) {
    this._gui.customer.rc.style.visibility = 'hidden';
  } else {
    this._gui.customer.rc.style.visibility = 'visible';
  }
  
  this._gui.order.type.disabled = false;
  this._gui.order.photo.disabled = false;
  this._gui.order.video.disabled = false;
  
  if (order.disposalstatus == 1 || order.finished == 1) {
    this._gui.order.total.disabled = true;
    this._gui.order.type.disabled = true;
    this._gui.order.photo.disabled = true;
    this._gui.order.video.disabled = true;
    this._gui.order.date.disabled = true;
    this._gui.order.add.style.display = 'none';
    this._gui.order.customerNote.disabled = true;
  } else {
    this._gui.order.total.disabled = false;
    this._gui.order.type.disabled = false;
    this._gui.order.photo.disabled = false;
    this._gui.order.video.disabled = false;
    this._gui.order.date.disabled = false;
    this._gui.order.add.style.display = 'inline';
    this._gui.order.customerNote.disabled = false;
  }
  
  /* 处理流程 */
  this._updateProduces();
  
  /* 删除订单 */
  if (!order.id && this._customer.orders.length > 1) {
    a = document.createElement('a');
    a.appendChild(document.createTextNode('Delete The Order'));
    a.href = '#';
    a.style.fontWeight = 'bold';
    a.style.fontSize = '120%';
    a.style.color = '#694247';
    a.onclick = function() { var r = window.confirm('DELETE THIS ORDER, CONTINUE?');
                               if (r) {
                                 MiscUtils.remove(_self._customer.orders, order, 'trackId');
                                 _self._orderTrackId = _self._customer.orders[0].trackId;
                                 _self._updateOrders.call(_self);
                               }
                             };
    this._gui.order.deleteDiv.appendChild(a);
  }
  
  /* 记住上一次选中的摄影套餐和摄像套餐 */
  //this._photo = (order.photo) ? order.photo : null;*/
  //this._video = (order.video) ? order.video : null;*/
  
    this._photo = { trackId: order.phototrackId,
		            name: order.photoname,
		            price: order.photoprice,
		            hours: order.photohours,
		            persons: order.photopersons,
		            makeup: order.photomakeup,
		            original: order.photooriginal
		          };
  
  this._video = {trackId: order.videotrackId,
		            name: order.videoname,
		            price: order.videoprice,
		            hours: order.videohours,
		            persons: order.videopersons,
		            makeup: order.videomakeup,
		            original: order.videooriginal
  };
  

  
  /* 订单总价 */
  this._gui.order.total.value = ( (parseFloat(order.price)).toFixed(2) > 0) ? (parseFloat(order.price)).toFixed(2) : '0.00';
  this._gui.order.total.onchange = function() { order.price = this.value;
                                                _self._updateOrders.call(_self);
                                              };
											  
								
											  
  var total = DlmanageUtils.total(order);
  
  //var total = this._gui.order.total.value;
  
  //alert((parseFloat(order.price)).toFixed(2));
  

  
  this._gui.order.percent.appendChild(document.createTextNode(((parseFloat(order.price)).toFixed(2) > 0) ? ' (Inc. ' + '{{$smarty.const.TAX|escape:javascript}}' + ' $' + (order.price * parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10)).toFixed(2) + ') ' : ''));
  if ((parseFloat(order.price)).toFixed(2) != parseFloat(total)) {
    var span = document.createElement('span');
    
    if((parseFloat(order.price)).toFixed(2) < parseFloat(total)){
      span.appendChild(document.createTextNode(' ' + (((total - (parseFloat(order.price)).toFixed(2)) / total) * 100).toFixed(1) + '% Off' ));
    }else{
      span.appendChild(document.createTextNode(' ' + 0 + '% Off' ));
    }
    
    this._gui.order.percent.appendChild(span);
    order.discount = (((total - (parseFloat(order.price)).toFixed(2)) / total) * 100).toFixed(2);
  }
  
  DOMUtils.removeChildElements(this._gui.order.dateTitle);
  if (order.type != '{{$smarty.const.ORDER_TYPE_WEDDING|escape:javascript}}') {
    this._gui.order.dateTitle.appendChild(document.createTextNode('Working Date'));
  } else {
    this._gui.order.dateTitle.appendChild(document.createTextNode('Wedding Date'));
  }
  
  /* 订单日期 */
  if (this._visitor && this._visitor.fromVisitor) {
    order.date = (this._visitor.weddingDay) ? this._visitor.weddingDay : '';
  }
      
  
  order.date =(order.date == 0)? '':(typeof(order.date) == 'string') ? SimpleDate.fromStamp(order.date) : order.date;  
  
  if(typeof(order.date) == "string")
  {
    this._gui.order.date.value = (order.date) ? SimpleDate.format(SimpleDate.fromStamp(order.date))  : '';
  }else{
    this._gui.order.date.value = (order.date) ? SimpleDate.format(order.date)  : '';
  }
  
   
  this.originalorder.push(order.flag+':'+SimpleDate.format(order.date));

  
   
  
  this._gui.order.date.onclick = function() { var context = this;
                                              _self._changeDate.call(_self, this, order.date, function(sd) { order.date = sd; _self._updateOrders.call(_self); }, false);
                                              
                                            };
  /* 订单类型 */
  
  this._gui.order.type.options[this._gui.order.type.options.length] = new Option('');
  for (var i = 0, il = OrderType.array().length; i < il; i++) {
    var orderType = OrderType.array()[i];
    option = new Option(orderType);
    this._gui.order.type.options[this._gui.order.type.options.length] = option;
    if (option.text == order.type) {
      this._gui.order.type.selectedIndex = this._gui.order.type.options.length - 1;
    }
  }
  
  this._gui.order.type.disabled = false;
  if (order.id) {
    this._gui.order.type.disabled = true;
  } else {
    this._gui.order.type._index = this._gui.order.type.selectedIndex;
    this._gui.order.type.onchange = function() {if (this._index == 0) {
                                                   order.flag = DlmanageUtils.flag(_self._customer, this.options[this.selectedIndex].text);
                                                   order.type = this.options[this.selectedIndex].text;
                                                   order.price = 0;
                                                   order.phototrackId = (this.options[this.selectedIndex]._package) ? this.options[this.selectedIndex]._package.trackId : '';
                                                   order.photoname = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).name : '';
                                                   order.photoprice = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).price : '0';
                                                   order.photohours = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).hours : '0';
                                                   order.photopersons = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).persons : '0';
                                                   order.photomakeup = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).makeup : '';
                                                   order.photooriginal = (this.options[this.selectedIndex]._package) ? DlmanageUtils.original(this.options[this.selectedIndex]._package) : '0';
                                                   
                                                   order.videotrackId = (this.options[this.selectedIndex]._package) ? this.options[this.selectedIndex]._package.trackId : '';
                                                   order.videoname = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).name : '';
                                                   order.videoprice = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).price : '0';
                                                   order.videohours = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).hours : '0';
                                                   order.videopersons = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).persons : '0';
                                                   order.videomakeup = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).makeup : '';
                                                   order.videooriginal = (this.options[this.selectedIndex]._package) ? DlmanageUtils.original(this.options[this.selectedIndex]._package) : '0';
                                                   order.purchaseItems = [];
                                                   //_self._photo = null;
                                                   //_self._video = null;
                                                   var produces = MiscUtils.find(_self._produces, [{ attrs: ['orderTrackId'], value: order.trackId }]);
                                                   for (var i = 0, il = produces.length; i < il; i++) {
                                                     var tmp = produces[i];
                                                     tmp._deleted = true;
                                                   }
												   
                                                   if (order.type == '{{$smarty.const.ORDER_TYPE_WEDDING|escape:javascript}}') {
                                                     var produce = Produce.instance();
                                                     produce.branch = '{{$smarty.const.BRANCH|escape:javascript}}';
                                                     produce.host = 'http://{{$smarty.const.DOMAIN|escape:javascript}}{{$smarty.const.PATH|escape:javascript}}';
                                                     produce.title = 'Pre Con';
                                                     produce.when = 0;
                                                     produce.orderTrackId = order.trackId;
                                                     produce.pi_trackId = MiscUtils.unique();
                                                     produce.pi_model = '{{$smarty.const.MODEL_NAME_PRECON|escape:javascript}}';
                                                     produce.pi_selects = [];
                                                     _self._produces[_self._produces.length] = produce;
                                                   }
                                                   _self._updateOrders.call(_self);
                                                 } else {
                                                   var r = window.confirm('CHANGE Type WILL CHANGE EVERYTHING, CONTINUE?');
                                                   if (r) {
                                                     order.flag = DlmanageUtils.flag(_self._customer, this.options[this.selectedIndex].text);
                                                     order.type = this.options[this.selectedIndex].text;
                                                     order.price = 0;
                                                     order.phototrackId = (this.options[this.selectedIndex]._package) ? this.options[this.selectedIndex]._package.trackId : '';
                                                     order.photoname = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).name : '';
                                                     order.photoprice = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).price : '0';
                                                     order.photohours = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).hours : '0';
                                                     order.photopersons = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).persons : '0';
                                                     order.photomakeup = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).makeup : '';
                                                     order.photooriginal = (this.options[this.selectedIndex]._package) ?  DlmanageUtils.original(this.options[this.selectedIndex]._package) : '0';
                                                   
                                                     order.videotrackId = (this.options[this.selectedIndex]._package) ? this.options[this.selectedIndex]._package.trackId : '';
                                                     order.videoname = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).name : '';
                                                     order.videoprice = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).price : '0';
                                                     order.videohours = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).hours : '0';
                                                     order.videopersons = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).persons : '0';
                                                     order.videomakeup = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).makeup :'';
                                                     order.videooriginal = (this.options[this.selectedIndex]._package) ?  DlmanageUtils.original(this.options[this.selectedIndex]._package) : '0';

                                                     order.purchaseItems = [];
                                                     //_self._photo = null;
                                                     //_self._video = null;
                                                     var produces = MiscUtils.find(_self._produces, [{ attrs: ['orderTrackId'], value: order.trackId }]);
                                                     for (var i = 0, il = produces.length; i < il; i++) {
                                                       var tmp = produces[i];
                                                       tmp._deleted = true;
                                                     }
													 
                                                     if (order.type == '{{$smarty.const.ORDER_TYPE_WEDDING|escape:javascript}}') {
                                                       var produce = Produce.instance();
                                                       produce.branch = '{{$smarty.const.BRANCH|escape:javascript}}';
                                                       produce.host = 'http://{{$smarty.const.DOMAIN|escape:javascript}}{{$smarty.const.PATH|escape:javascript}}';
                                                       produce.title = 'Pre Con';
                                                       produce.when = 0;
                                                       produce.orderTrackId = order.trackId;
                                                       produce.pi_trackId = MiscUtils.unique();
                                                       produce.pi_model = '{{$smarty.const.MODEL_NAME_PRECON|escape:javascript}}';
                                                       produce.pi_selects = [];
                                                       _self._produces[_self._produces.length] = produce;
													   
                                                     }
                                                     _self._updateOrders.call(_self);
                                                   } else {
                                                     this.selectedIndex = this._index;
                                                   }
                                                 }
                                               };
  }
  
  /* 摄影套餐 */
  
  
  this._gui.order.photo.options[this._gui.order.photo.options.length] = new Option('');
  
  for (var i = 0, il = this._packages.length; i < il; i++) {
    var package = this._packages[i];
	
    if (package.type != '{{$smarty.const.PACKAGE_TYPE_PHOTO|escape:javascript}}' || package.orderInc != order.type) {
      continue;
    }
    option = new Option(package.name + ' ($' + package.price + ')');
    option._package = package;
    
    this._gui.order.photo.options[this._gui.order.photo.options.length] = option;
    
    if (package.trackId == order.phototrackId) { 
      this._gui.order.photo.selectedIndex = this._gui.order.photo.options.length - 1;
    }
  }
  this._gui.order.photo._index = this._gui.order.photo.selectedIndex;
  this._gui.order.photo.onchange = function() { if (this._index == 0) {
                                                  var limit = (_self._photo) ? parseFloat(_self._photo.price, 10) : 0;
                                                  for (var i = 0, il = order.purchaseItems.length; i < il; i++) {
                                                    var p = order.purchaseItems[i];
                                                    if (p.packageInc == Constant.PACKAGE_TYPE_PHOTO && p.cancelled == 1) {
                                                      limit -= parseFloat(p.total, 10);
                                                    }
                                                  }
                                                  order.price -= (limit < 0) ? 0 : limit;
                                                  order.price += (this.options[this.selectedIndex]._package) ? parseFloat(this.options[this.selectedIndex]._package.price, 10) : 0;
                                                  //order.photo = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package) : MyOrderPackage.instance();
                                                  
                                                  order.phototrackId = (this.options[this.selectedIndex]._package) ? this.options[this.selectedIndex]._package.trackId : '';
                                                  order.photoname = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).name : '';
                                                  order.photoprice = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).price : '0';
												  order.photohours = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).hours : '0';
												  order.photopersons = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).persons : '0';
												  order.photomakeup = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).makeup : '';
												  //order.photooriginal = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).original : '0';
                                                  
                                                  order.photooriginal = (this.options[this.selectedIndex]._package) ?  DlmanageUtils.original(this.options[this.selectedIndex]._package) : '0';
                                                  
                                                  
                                                  _self._addPackagePurchases.call(_self, '{{$smarty.const.PACKAGE_TYPE_PHOTO|escape:javascript}}', this.options[this.selectedIndex]._package);
                                                  //_self._photo = order.photo;
                                                  
                                                  _self._updateOrders.call(_self);
                                                } else {
                                                  var r = window.confirm('CHANGE Type WILL CHANGE Products, CONTINUE?');
                                                  if (r) {
                                                    var limit = (_self._photo) ? parseFloat(_self._photo.price, 10) : 0;
                                                    for (var i = 0, il = order.purchaseItems.length; i < il; i++) {
                                                      var p = order.purchaseItems[i];
                                                      if (p.packageInc == Constant.PACKAGE_TYPE_PHOTO && p.cancelled == 1) {
                                                        limit -= parseFloat(p.total, 10);
                                                      }
                                                    }
                                                    order.price -= (limit < 0) ? 0 : limit;
                                                    order.price += (this.options[this.selectedIndex]._package) ? parseFloat(this.options[this.selectedIndex]._package.price, 10) : 0;
                                                    //order.photo = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package) : v1myorderpackage.instance();
                                                    order.phototrackId = (this.options[this.selectedIndex]._package) ? this.options[this.selectedIndex]._package.trackId : '';
                                                    order.photoname = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).name : '';
                                                    order.photoprice = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).price : '0';
												    order.photohours = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).hours : '0';
												    order.photopersons = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).persons : '0';
												    order.photomakeup = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).makeup : '';
												    order.photooriginal = (this.options[this.selectedIndex]._package) ?  DlmanageUtils.original(this.options[this.selectedIndex]._package) : '0';
                                                    
                                                    _self._addPackagePurchases.call(_self, '{{$smarty.const.PACKAGE_TYPE_PHOTO|escape:javascript}}', this.options[this.selectedIndex]._package);
                                                    //_self._photo = order.photo;
                                                    _self._updateOrders.call(_self);
                                                  } else {
                                                    this.selectedIndex = this._index;
                                                  }
                                                }
                                              };
  var pp = MiscUtils.find(this._produces, [{ attrs: ['orderTrackId'], value: order.trackId }, { attrs: ['pi_model'], value: Constant.MODEL_NAME_USER_PHOTOGRAPHER }, { attrs: ['_deleted'], value: undefined }]);
  for (var i = 0, il = pp.length; i < il; i++) {
    var tmp = pp[i];
    if (tmp.state > 3) {
      this._gui.order.photo.disabled = true;
    }
  }
  
  /* 摄像套餐 */
  this._gui.order.video.options[this._gui.order.video.options.length] = new Option('');
  for (var i = 0, il = this._packages.length; i < il; i++) {
    var package = this._packages[i];
    if (package.type != '{{$smarty.const.PACKAGE_TYPE_VIDEO|escape:javascript}}' || package.orderInc != order.type) {
      continue;
    }
    option = new Option(package.name + ' ($' + package.price + ')');
    option._package = package;
    this._gui.order.video.options[this._gui.order.video.options.length] = option;
    if (package.trackId == order.videotrackId) {
      this._gui.order.video.selectedIndex = this._gui.order.video.options.length - 1;
    }
  }
  this._gui.order.video._index = this._gui.order.video.selectedIndex;
  this._gui.order.video.onchange = function() { if (this._index == 0) {
                                                  var conditions = [{ attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_MUSIC|escape:javascript}}' }, { attrs: ['orderTrackId'], value: order.trackId }, { attrs: ['_deleted'], value: undefined }, { attrs: ['cancelled'], value: 0 }]
                                                  if (this.options[this.selectedIndex]._package && _self._video.name == '') {
                                                    var produces = MiscUtils.find(_self._produces, conditions);
                                                    if (produces.length == 0) {
                                                      var produce = Produce.instance();
                                                      produce.branch = '{{$smarty.const.BRANCH|escape:javascript}}';
                                                      produce.host = 'http://{{$smarty.const.DOMAIN|escape:javascript}}{{$smarty.const.PATH|escape:javascript}}';
                                                      produce.title = 'Video Music';
                                                      produce.when = 0;
                                                      produce.orderTrackId = order.trackId;
                                                      produce.pi_trackId = MiscUtils.unique();
                                                      produce.pi_model = '{{$smarty.const.MODEL_NAME_MUSIC|escape:javascript}}';
                                                      produce.pi_selects = [];
                                                      _self._produces[_self._produces.length] = produce;
                                                    }
                                                  } else if (!this.options[this.selectedIndex]._package && _self._video.name != '') {
                                                    var produces = MiscUtils.find(_self._produces, conditions);
                                                    if (produces.length == 1) {
                                                      if (produces[0].id) {
                                                        produces[0].cancelled = 1;
                                                      } else {
                                                        produces[0]._deleted = true;
                                                      }
                                                    }
                                                  }
                                                  var conditions = [{ attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_REQUIREMENT|escape:javascript}}' }, { attrs: ['orderTrackId'], value: order.trackId }, { attrs: ['_deleted'], value: undefined }, { attrs: ['cancelled'], value: 0 }]
                                                  if (this.options[this.selectedIndex]._package && _self._video.name == '') {
                                                    var produces = MiscUtils.find(_self._produces, conditions);
                                                    if (produces.length == 0) {
                                                      var produce = Produce.instance();
                                                      produce.branch = '{{$smarty.const.BRANCH|escape:javascript}}';
                                                      produce.host = 'http://{{$smarty.const.DOMAIN|escape:javascript}}{{$smarty.const.PATH|escape:javascript}}';
                                                      produce.title = 'Video Requirement Form';
                                                      produce.when = 0;
                                                      produce.orderTrackId = order.trackId;
                                                      produce.pi_trackId = MiscUtils.unique();
                                                      produce.pi_model = '{{$smarty.const.MODEL_NAME_REQUIREMENT|escape:javascript}}';
                                                      produce.pi_selects = [];
                                                      _self._produces[_self._produces.length] = produce;
                                                    }
                                                  } else if (!this.options[this.selectedIndex]._package && _self._video.name != '') {
                                                    var produces = MiscUtils.find(_self._produces, conditions);
                                                    if (produces.length == 1) {
                                                      if (produces[0].id) {
                                                        produces[0].cancelled = 1;
                                                      } else {
                                                        produces[0]._deleted = true;
                                                      }
                                                    }
                                                  }
                                                  var limit = (_self._video) ? parseFloat(_self._video.price, 10) : 0;
                                                  for (var i = 0, il = order.purchaseItems.length; i < il; i++) {
                                                    var p = order.purchaseItems[i];
                                                    if (p.packageInc == Constant.PACKAGE_TYPE_VIDEO && p.cancelled == 1) {
                                                      limit -= parseFloat(p.total, 10);
                                                    }
                                                  }
                                                  order.price -= (limit < 0) ? 0 : limit;
                                                  order.price += (this.options[this.selectedIndex]._package) ? parseFloat(this.options[this.selectedIndex]._package.price, 10) : 0;
                                                  //order.video = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package) : v1myorderpackage.instance();
                                                  order.videotrackId = (this.options[this.selectedIndex]._package) ? this.options[this.selectedIndex]._package.trackId : '';
                                                  order.videoname = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).name : '';
                                                  order.videoprice = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).price : '0';
												  order.videohours = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).hours : '0';
												  order.videopersons = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).persons : '0';
												  order.videomakeup = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).makeup : '';
												  order.videooriginal =(this.options[this.selectedIndex]._package) ? DlmanageUtils.original(this.options[this.selectedIndex]._package) : '0';
                                                  
                                                  _self._addPackagePurchases.call(_self, '{{$smarty.const.PACKAGE_TYPE_VIDEO|escape:javascript}}', this.options[this.selectedIndex]._package);
                                                  //_self._video = order.video;
                                                  _self._updateOrders.call(_self);
                                                } else {
                                                  var r = window.confirm('CHANGE Type WILL CHANGE Products, CONTINUE?');
                                                  if (r) {
                                                    var conditions = [{ attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_MUSIC|escape:javascript}}' }, { attrs: ['orderTrackId'], value: order.trackId }, { attrs: ['_deleted'], value: undefined }, { attrs: ['cancelled'], value: 0 }]
                                                    if (this.options[this.selectedIndex]._package && _self._video.name == '') {
                                                      var produces = MiscUtils.find(_self._produces, conditions);
                                                      if (produces.length == 0) {
                                                        var produce = Produce.instance();
                                                        produce.branch = '{{$smarty.const.BRANCH|escape:javascript}}';
                                                        produce.host = 'http://{{$smarty.const.DOMAIN|escape:javascript}}{{$smarty.const.PATH|escape:javascript}}';
                                                        produce.title = 'Video Music';
                                                        produce.when = 0;
                                                        produce.orderTrackId = order.trackId;
                                                        produce.pi_trackId = MiscUtils.unique();
                                                        produce.pi_model = '{{$smarty.const.MODEL_NAME_MUSIC|escape:javascript}}';
                                                        produce.pi_selects = [];
                                                        _self._produces[_self._produces.length] = produce;
                                                      }
                                                    } else if (!this.options[this.selectedIndex]._package && _self._video.name != '') {
                                                      var produces = MiscUtils.find(_self._produces, conditions);
                                                      if (produces.length == 1) {
                                                        if (produces[0].id) {
                                                          produces[0].cancelled = 1;
                                                        } else {
                                                          produces[0]._deleted = true;
                                                        }
                                                      }
                                                    }
                                                    var conditions = [{ attrs: ['pi_model'], value: '{{$smarty.const.MODEL_NAME_REQUIREMENT|escape:javascript}}' }, { attrs: ['orderTrackId'], value: order.trackId }, { attrs: ['_deleted'], value: undefined }, { attrs: ['cancelled'], value: 0 }]
                                                    if (this.options[this.selectedIndex]._package && _self._videoname == '') {
                                                      var produces = MiscUtils.find(_self._produces, conditions);
                                                      if (produces.length == 0) {
                                                        var produce = Produce.instance();
                                                        produce.branch = '{{$smarty.const.BRANCH|escape:javascript}}';
                                                        produce.host = 'http://{{$smarty.const.DOMAIN|escape:javascript}}{{$smarty.const.PATH|escape:javascript}}';
                                                        produce.title = 'Video Requirement Form';
                                                        produce.when = 0;
                                                        produce.orderTrackId = order.trackId;
                                                        produce.pi_trackId = MiscUtils.unique();
                                                        produce.pi_model = '{{$smarty.const.MODEL_NAME_REQUIREMENT|escape:javascript}}';
                                                        produce.pi_selects = [];
                                                        _self._produces[_self._produces.length] = produce;
                                                      }
                                                    } else if (!this.options[this.selectedIndex]._package && _self._videoname != '') {
                                                      var produces = MiscUtils.find(_self._produces, conditions);
                                                      if (produces.length == 1) {
                                                        if (produces[0].id) {
                                                          produces[0].cancelled = 1;
                                                        } else {
                                                          produces[0]._deleted = true;
                                                        }
                                                      }
                                                    }
                                                    var limit = (_self._video) ? parseFloat(_self._video.price, 10) : 0;
                                                    for (var i = 0, il = order.purchaseItems.length; i < il; i++) {
                                                      var p = order.purchaseItems[i];
                                                      if (p.packageInc == Constant.PACKAGE_TYPE_VIDEO && p.cancelled == 1) {
                                                        limit -= parseFloat(p.total, 10);
                                                      }
                                                    }
                                                    order.price -= (limit < 0) ? 0 : limit;
                                                    order.price += (this.options[this.selectedIndex]._package) ? parseFloat(this.options[this.selectedIndex]._package.price, 10) : 0;
                                                    //order.video = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package) : MyOrderPackage.instance();
                                                    order.videotrackId = (this.options[this.selectedIndex]._package) ? this.options[this.selectedIndex]._package.trackId : '';
                                                    order.videoname = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).name : '';
                                                    order.videoprice = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).price : '0';
												    order.videohours = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).hours : '0';
												    order.videopersons = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).persons : '0';
												    order.videomakeup = (this.options[this.selectedIndex]._package) ? DlmanageUtils.renew(this.options[this.selectedIndex]._package).makeup : '';
												    order.videooriginal = (this.options[this.selectedIndex]._package) ? DlmanageUtils.original(this.options[this.selectedIndex]._package) : '0';
                                                    _self._addPackagePurchases.call(_self, '{{$smarty.const.PACKAGE_TYPE_VIDEO|escape:javascript}}', this.options[this.selectedIndex]._package);
                                                    //_self._video = order.video;
                                                    _self._updateOrders.call(_self);
                                                  } else {
                                                    this.selectedIndex = this._index;
                                                  }
                                                }
                                              };
  var vp = MiscUtils.find(this._produces, [{ attrs: ['orderTrackId'], value: order.trackId }, { attrs: ['pi_model'], value: Constant.MODEL_NAME_USER_VIDEOGRAPHER }, { attrs: ['_deleted'], value: undefined }]);
  for (var i = 0, il = vp.length; i < il; i++) {
    var tmp = vp[i];
    if (tmp.state > 3) {
      this._gui.order.video.disabled = true;
    }
  }

  /* Package的名称和价格 */
  this._gui.order.pvpackage.appendChild(document.createTextNode((order.photoname != '') ? '(Photo) ' + order.photoname + ': ' : ''));
  var span = document.createElement('span');
  span.style.color = '#000';
  span.appendChild(document.createTextNode((order.photoprice != 0) ? '$' + parseFloat(order.photoprice, 10).toFixed(2) : ''));
  this._gui.order.pvpackage.appendChild(span);
  var span = document.createElement('span');
  span.appendChild(document.createTextNode((order.videoname != '') ? ', (Video) ' + order.videoname + ': ' : ''));
  this._gui.order.pvpackage.appendChild(span);
  var span = document.createElement('span');
  span.style.color = '#000';
  span.appendChild(document.createTextNode((order.videoprice != 0) ? '$' + parseFloat(order.videoprice, 10).toFixed(2) : ''));
  this._gui.order.pvpackage.appendChild(span);

  /* Package Include & Addtions */
  new TablePurchases(this._gui.order.pPurchases, MiscUtils.find(order.purchaseItems, [{ attrs: ['packageInc'], value: '', unequal: true }]), { parent: order, samples: this._samples, callbackFunc: function() { _self._updateOrders.call(_self); }, hidePrice: true, noEdit: true, cancelOnly: true});
  new TablePurchases(this._gui.order.aPurchases, MiscUtils.find(order.purchaseItems, [{ attrs: ['packageInc'], value: '' }]), { parent: order, samples: this._samples, callbackFunc: function() { _self._updateOrders.call(_self); } });
  
  /* PV表 */
  var pv = MiscUtils.find(this._produces, [{ attrs: ['orderTrackId'], value: order.trackId }, { attrs: ['_deleted'], value: undefined }, { attrs: ['cancelled'], value: false }]);
   
  pv = MiscUtils.find(pv, [{ attrs: ['pi_model'], value: Constant.MODEL_NAME_USER_PHOTOGRAPHER }, { attrs: ['pi_model'], value: Constant.MODEL_NAME_USER_VIDEOGRAPHER },{ attrs: ['pi_model'], value: Constant.MODEL_NAME_USER_LIGHT },{ attrs: ['pi_model'], value: Constant.MODEL_NAME_USER_MAKEUP }], MiscUtils.disjunction);
  
  
  if (pv.length != 0) {
    new IContainerWorkers(this._gui.order.tasks, pv, { callbackFunc: function() { _self._updateOrders.call(_self); }, purchases: order.purchaseItems, parent: order });
  }
  
  /* 添加附加产品 */
  this._gui.order.add.onclick = function() { var purchase = v1purchaseitem.instance();
                                             purchase.sampleId = _self._samples[0].id;
                                             purchase.name = _self._samples[0].name;
                                             purchase.price = _self._samples[0].price;
                                             purchase.type = _self._samples[0].type;
                                             purchase.unit = _self._samples[0].unit;
                                             purchase.model = _self._samples[0].model;
                                             purchase.otherId = MiscUtils.unique();
                                             purchase.selects = [];
											 purchase.notes = [];
                                             for (var i = 0, il = _self._samples[0].attributes.length; i < il; i++) {
                                               var attribute = _self._samples[0].attributes[i];
                                               DlmanageUtils.purchaseChoose(purchase, attribute, attribute.options[0]);
                                             }
                                             purchase.quantity = 1;
                                             purchase.isFree = (DlmanageUtils.isFirstAlbum(order.purchaseItems, purchase)) ? 0 : 1;
                                             purchase.total = parseFloat(purchase.price) + ((purchase.isFree && (purchase.type == '{{$smarty.const.SAMPLE_TYPE_ALBUM|escape:javascript}}' || purchase.type == '{{$smarty.const.SAMPLE_TYPE_MAGAZINE|escape:javascript}}')) ? parseFloat(180) : parseFloat(0));
                                             purchase.packageInc = '';
                                             _self._addPurchase.call(_self, purchase, DOMUtils.findPos(this));
                                           };
  
  /* Customer Note */
  this._gui.order.customerNote.value = order.note;
  this._gui.order.customerNote.onchange = function() { order.note = this.value; };
};
/* 
 * 选择套餐
 */
HandlePageCustomer.prototype._addPackagePurchases = function(packageType, package) {
  var i, p, v, purchase, order = DlmanageUtils.order(this._customer.orders, this._orderTrackId);
  
  /* 删除之前的套餐产品 */
  i = 0;
  while (i < order.purchaseItems.length) {
    var purchase = order.purchaseItems[i];
    if (packageType == purchase.packageInc) {
      order.purchaseItems.splice(i, 1);
      i = 0;
    } else {
      i++;
    }
  }

  if (package) {
    /* 把所有套餐的产品天添加到订单的产品列表中 */
    var stamp = MiscUtils.unique();
    for (var i = 0, il = package.packageItems.length; i < il; i++) {
      var purchase = v1purchaseitem.instance();
      purchase.selects = [];
	  purchase.notes = [];	  
      purchase.sampleId = package.packageItems[i].sample.id;
      purchase.name = package.packageItems[i].sample.name;
      purchase.price = package.packageItems[i].sample.price;
      purchase.type = package.packageItems[i].sample.type;
      purchase.unit = package.packageItems[i].sample.unit;
      purchase.model = package.packageItems[i].sample.model;
      purchase.selects = DlmanageUtils.renew(package.packageItems[i].selects);
      purchase.quantity = package.packageItems[i].quantity;
      purchase.total = package.packageItems[i].quantity * package.packageItems[i].sample.price;
      purchase.otherId = package.packageItems[i].otherId;
      purchase.packageInc = packageType;
      purchase.isFree = 0;

      order.purchaseItems[order.purchaseItems.length] = purchase;
    }
    
    /* 从后台产品中找出摄影师、摄像师和化妆师。把化妆师添加到订单的产品列表中 */
    if (package.makeup != '') {
      var purchase = v1purchaseitem.instance();
      purchase.sampleId = DlmanageUtils.sampleId(this._samples, package);
      purchase.name = (package.makeup == '{{$smarty.const.SAMPLE_TYPE_MAKEUP_AT_STUDIO|escape:javascript}}') ? 'Makeup In House' : (package.makeup == '{{$smarty.const.SAMPLE_TYPE_MAKEUP_AT_HOME|escape:javascript}}') ? 'Makeup At Home' : 'Makeup Full Day';
      purchase.price = (package.makeup == '{{$smarty.const.SAMPLE_TYPE_MAKEUP_AT_STUDIO|escape:javascript}}') ? '{{$smarty.const.CUSTOM_PRICE_MAKEUP_AT_STUDIO|escape:javascript}}' : (package.makeup == '{{$smarty.const.SAMPLE_TYPE_MAKEUP_AT_HOME|escape:javascript}}') ? '{{$smarty.const.CUSTOM_PRICE_MAKEUP_AT_HOME|escape:javascript}}' : '{{$smarty.const.CUSTOM_PRICE_MAKEUP_FULL_DAY|escape:javascript}}';
      purchase.type = package.makeup;
      purchase.unit = '';
      purchase.model = '{{$smarty.const.MODEL_NAME_USER_MAKEUP|escape:javascript}}';
      purchase.otherId = MiscUtils.unique();
      purchase.selects = [];
	  purchase.notes = [];
      purchase.quantity = 1;
      purchase.total = purchase.price;
      purchase.packageInc = packageType;
      order.purchaseItems[order.purchaseItems.length] = purchase;
    }
    
    for (var i = 0, il = package.persons; i < il; i++) {
      var purchase = v1purchaseitem.instance();
	  purchase.selects = [];
	  purchase.notes = [];
      purchase.sampleId = DlmanageUtils.sampleId(this._samples, package);
      purchase.name = (package.type == '{{$smarty.const.PACKAGE_TYPE_PHOTO|escape:javascript}}') ? 'Single Photographer With Hours' : 'Single Videographer With Hours';
      purchase.price = (package.type == '{{$smarty.const.PACKAGE_TYPE_PHOTO|escape:javascript}}') ? '{{$smarty.const.CUSTOM_PRICE_PHOTOGRAPHER|escape:javascript}}' : '{{$smarty.const.CUSTOM_PRICE_VIDEOGRAPHER|escape:javascript}}' ;
      purchase.type = (package.type == '{{$smarty.const.PACKAGE_TYPE_PHOTO|escape:javascript}}') ? '{{$smarty.const.SAMPLE_TYPE_PHOTOGRAPHER_WITH_HOURS|escape:javascript}}' : '{{$smarty.const.SAMPLE_TYPE_VIDEOGRAPHER_WITH_HOURS|escape:javascript}}' ;
      purchase.unit = '';
      purchase.model = (package.type == '{{$smarty.const.PACKAGE_TYPE_PHOTO|escape:javascript}}') ? '{{$smarty.const.MODEL_NAME_USER_PHOTOGRAPHER|escape:javascript}}' : '{{$smarty.const.MODEL_NAME_USER_VIDEOGRAPHER|escape:javascript}}' ;
      purchase.otherId = MiscUtils.unique();
      if (package.type == '{{$smarty.const.PACKAGE_TYPE_PHOTO|escape:javascript}}') {
        var select = v1purchaseitemselect.instance();
        select.title = 'Fav';
        select.value = '0';
        purchase.selects[purchase.selects.length] = select;
      } else {
        purchase.selects = [];
      }
      purchase.quantity = package.hours;
      purchase.total = purchase.price * purchase.quantity;
      purchase.packageInc = packageType;
      order.purchaseItems[order.purchaseItems.length] = purchase;
      
    }
  }
};
/* 
 * 添加附加产品
 */
HandlePageCustomer.prototype._addPurchase = function(purchase, pos) {
  var div, order = DlmanageUtils.order(this._customer.orders, this._orderTrackId), _self = this;
  
  /* OK & Cancel */
  var func1 = function() { _self._popupBox2._close();
                           order.purchaseItems[order.purchaseItems.length] = purchase;
                           /* 改变价格 */
                           order.price = parseFloat(order.price, 10) + parseFloat(purchase.total, 10);
                           _self._updateOrders.call(_self);
                         };
  var func2 = function() { _self._popupBox2._close(); };
  
  var div = document.createElement('div');
  pos = [pos[0] - 600, pos[1] - 80];
  MiscUtils.dialog(this._popupBox2, pos, div, func1, func2, {t: 'Add Additions', width: 800, height: 150});
  var iContainerPurchase = new IContainerPurchase(div, purchase, { callbackFunc: function() { iContainerPurchase._createElements(); }, parent: order, samples: this._samples, isAdd: true });
};
/* 
 * 处理流程
 */
HandlePageCustomer.prototype._updateProduces = function() {
  var order = DlmanageUtils.order(this._customer.orders, this._orderTrackId);
  var options = { orderDate: order.date,
                  user: this._operator,
                  date: this._now,
                  orderFlag: order.flag,
                  orderTrackId: order.trackId,
                  orderId: order.id,
                  customerId: this._customer.id,
                  result: (this._newresult) ? this._newresult : null
                };
                
				
  this._newresult = DlmanageUtils.synchronise(order.purchaseItems, this._produces, options);

  this._produces = this._newresult.produces;
};
/* 
 * 修改日期1Change
 */
HandlePageCustomer.prototype._changeDate = function(label, currentDate, onChangeFunc, showTime) {
  var div, _self = this;
  div = document.createElement('div');
  div.style.left = DOMUtils.findPos(label)[0] + 'px';
  div.style.top = DOMUtils.findPos(label)[1] + 'px';
  this._popupBox._open(div, { pos: DOMUtils.findPos(label) });
  
  var ds = new DateSelect(currentDate, null, { container: div, showTime: showTime });
  ds._selectFunc = function(sd) { _self._selectDate.call(_self, sd, label, onChangeFunc, showTime); };
};
/* 
 * 修改日期2Select
 */
HandlePageCustomer.prototype._selectDate = function(sd, label, onChangeFunc, showTime) {
  onChangeFunc(sd);
  this._showDate(sd, label, showTime);
  this._popupBox._close();
};
/* 
 * 修改日期3Show
 */
HandlePageCustomer.prototype._showDate = function(sd, label, showTime) {
  if (sd) { 
    label.value = SimpleDate.format(sd, showTime);
  } else {
    label.value = '';
  }
};
